package com.jftone.bh.app.action;

import com.jftone.bh.app.config.SysConst;
import com.jftone.bh.app.model.NtbUser;
import com.jftone.bh.app.service.UserService;
import org.apache.log4j.Logger;
import org.jftone.annotation.Autowired;
import org.jftone.annotation.Controller;
import org.jftone.exception.ActionException;
import org.jftone.util.DataMap;
import org.jftone.util.IData;
import org.jftone.util.StringUtil;

import javax.servlet.http.HttpSession;
import java.util.List;


@Controller(mapping="/userAction")
public class UserAction extends BaseAction {

	private Logger logger = Logger.getLogger(UserAction.class);
	
	@Autowired
	private UserService service;

	public void query() throws ActionException {
		IData<String, Object> data = getData();
		String account = data.getString("account", "");
		String userName = data.getString("userName", "");
		String uplevel = data.getString("uplevel", "");
		String hphm = data.getString("hphm", "");
		this.putRenderInfo("hphm", hphm);
		this.putRenderInfo("uplevel", uplevel);
		this.putRenderInfo("userName", userName);
		this.putRenderInfo("account", account);
		try {
			//设置功能按钮
			setMenuFuncDerictive();
			//设置分页组件
			setPageDerictive();
			//设置隐藏传值参数
			putParam("account", account);
			putParam("userName", userName);
			putParam("uplevel", uplevel);
			putParam("hphm", hphm);
			putParam(SysConst.QUERY_PARAMS, getParam());
			List<IData<String, Object>> list = service.queryList(data, getPage());
			this.putRenderInfo("list", list);
			this.render("user/user_list.html");
		} catch (Exception e) {
			logger.error("查询用户列表错误" + e.getMessage(), e);
			throw new ActionException("查询用户列表错误" + e.getMessage(), e);
		}
	}
	public void queryUserAlert() throws ActionException {
		IData<String, Object> data = getData();
		String account = data.getString("account", "");
		String userName = data.getString("userName", "");
		this.putRenderInfo("userName", userName);
		this.putRenderInfo("account", account);
		try {
			setPageDerictive();
			List<IData<String, Object>> list = service.queryList(data, getPage());
			this.putRenderInfo("userList", list);
			this.render("user/query_user_alert.html");
		} catch (Exception e) {
			logger.error("查询用户列表错误" + e.getMessage(), e);
			throw new ActionException("查询用户列表错误" + e.getMessage(), e);
		}
	}
	public void delete() throws ActionException{
		IData<String, Object> data = getData();
		String dataId = data.getString("dataId", "");
		if(StringUtil.isBlank(dataId) || !StringUtil.isNumber(dataId)){
			this.alertMsg("参数传值错误");
			return;
		}
		try {
			int resultCode = service.delete(Long.parseLong(dataId));
			if(resultCode == 0){
				this.showMsg("操作成功", "?method=query");
			}else if(resultCode == 1){
				this.showMsg("删除失败", "?method=query");
			}
		} catch (Exception e) {
			logger.error("操作数据错误"+e.getMessage(), e);
			throw new ActionException("操作数据错误"+e.getMessage(), e);
		}
	}

	public void regain() throws ActionException{
		IData<String, Object> data = getData();
		String dataId = data.getString("dataId", "");
		if(StringUtil.isBlank(dataId) || !StringUtil.isNumber(dataId)){
			this.alertMsg("参数传值错误");
			return;
		}
		try {
			int resultCode = service.regain(Long.parseLong(dataId));
			if(resultCode == 0){
				this.showMsg("操作成功", "?method=query");
			}else if(resultCode == 1){
				this.showMsg("操作失败", "?method=query");
			}
		} catch (Exception e) {
			logger.error("操作数据错误"+e.getMessage(), e);
			throw new ActionException("操作数据错误"+e.getMessage(), e);
		}
	}

	public void resetLoginPass() throws ActionException{
		IData<String, Object> data = getData();
		String dataId = data.getString("dataId", "");
		if(StringUtil.isBlank(dataId) || !StringUtil.isNumber(dataId)){
			this.alertMsg("参数传值错误");
			return;
		}
		try {
			int resultCode = service.resetLoginPass(Long.parseLong(dataId));
			if(resultCode == 0){
				this.showMsg("操作成功", "?method=query");
			}else if(resultCode == 1){
				this.showMsg("操作失败", "?method=query");
			}
		} catch (Exception e) {
			logger.error("操作数据错误"+e.getMessage(), e);
			throw new ActionException("操作数据错误"+e.getMessage(), e);
		}
	}

	public void moneyOper() throws ActionException {
		IData<String, Object> data = getData();
		String dataId = data.getString("dataId");
		if(StringUtil.isBlank(dataId) || !StringUtil.isNumber(dataId)){
			this.alertMsg("参数传值错误");
			return;
		}
		try {
			IData<String, Object> inData = new DataMap<String, Object>();
			inData.put("id",dataId);
			NtbUser user = service.getNtbUser(inData);
			this.putRenderInfo("user", user);
			this.render("user/money_oper.html");
		} catch (Exception e) {
			logger.error("修改用户资金数据错误"+e.getMessage(), e);
			throw new ActionException("修改用户资金数据错误"+e.getMessage(), e);
		}
	}

	public void saveMoneyOper() throws ActionException{
		IData<String, Object> data = getData();
		String dataId = data.getString("dataId");
		if(StringUtil.isBlank(dataId) || !StringUtil.isNumber(dataId)){
			this.alertMsg("参数传值错误");
			return;
		}
		try {
			HttpSession session = this.getRequest().getSession(false);
			IData<String, Object> admin = (IData<String, Object>)session.getAttribute(SysConst.ADMIN_USER);
			int resultCode = service.saveMoneyOper(data,admin.getString("adminUser"));
			if(resultCode == 0){
				this.showMsg("操作成功", "?method=query");
			}else{
				this.errorMsg("操作失败","操作失败","操作失败", "?method=query");
			}
		} catch (Exception e) {
			logger.error("用户资金数据修改错误"+e.getMessage(), e);
			throw new ActionException("用户资金数修改据错误"+e.getMessage(), e);
		}
	}
}
